<?php
namespace helper;
class Checker {
	// 判断IPv6地址
	public static function isIpv6($input){
		return filter_var($input, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
	}
	// 判断IPv4地址
	public static function isIpv4($input){
		return filter_var($input, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
	}
	// 判断IP地址
	public static function isIp($input){
		return (self::isIpv6($input) or self::isIpv4($input));
	}
	// 判断域名
	public static function isDomain($input){
		return preg_match('/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/', $input);
	}
	// 判断URL
	public static function isUrl($url) {
		return filter_var($url, FILTER_VALIDATE_URL) !== false;
	}
	// 判断电子邮件地址
	public static function isEmail($email) {
		return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
	}
	// 判断手机号码
	public static function isMobile($phoneNumber){
		return preg_match('/^1[3-9]\d{9}$/', $phoneNumber) === 1;
	}
	
	
	// 判断闭包
	public static function isClosure($value){
		return $value instanceof \Closure;
	}
	// 判断回调函数
	public static function isCallable($value){
		return ($value instanceof \Closure or is_callable($value) or (is_array($value) and is_object($value[0]) and method_exists($value[0], $value[1])));
	}
	// 判断颜色值
	public static function isColor($color) {
		return (self::isHexColor($color) or self::isRgb($color) or self::isRgba($color));
	}
	// 判断十六进制颜色值
	public static function isHexColor($color) {
		return preg_match('/^#?([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/', $color) === 1;
	}
	// 判断rgba颜色值
	public static function isRgba($input) {
		$pattern = '/^rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3}|[0-9]{1,2}\.[0-9]+)\s*\)$/';
		if (preg_match($pattern, $input, $matches)) {
			for ($i = 1; $i <= 3; $i++) {
				if ($matches[$i] < 0 || $matches[$i] > 255) {
					return false;
				}
			}
			$alpha = floatval($matches[4]);
			if ($alpha < 0 || $alpha > 1) {
				return false;
			}
			return true;
		}
		return false;
	}
	// 判断rgb颜色值
	public static function isRgb($input) {
		$pattern = '/^rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)$/';
		if (preg_match($pattern, $input, $matches)) {
	        for ($i = 1; $i <= 3; $i++) {
	            if ($matches[$i] < 0 || $matches[$i] > 255) {
	                return false;
	            }
	        }
			return true;
		}
		return false;
	}
}